package pdfsetter.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;

import pdfsetter.core.pages.PageInfo;
import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.pages.PageType;
import pdfsetter.core.schema.AutomaticDocumentSchemaPattern;
import pdfsetter.core.schema.DocumentSchema;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.core.schema.advanced.FilePageSelector;
import pdfsetter.core.schema.onefile.OneFileDocumentSchemaPattern;
import pdfsetter.core.schema.onefile.SimpleOneFileDocumentSchemaPattern;
import pdfsetter.core.schema.selectors.CompoundFilePageSelector;
import pdfsetter.core.schema.selectors.LeapingPageSelector;
import pdfsetter.core.schema.selectors.RangePageSelector;

public class AutomaticDocumentSchemaExample {
    public static void main(String[] args) {
        // ----------- Create SinglePageSchemas -----------
        PageInfo a4page = new PageInfo(PageType.A4).rotate();

        ArrayList<PageRectangle> locations2 = new ArrayList<PageRectangle>();
        locations2.add(new PageRectangle(a4page.getWidth() / 4, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        SinglePageSchema pageSchema1 = new SinglePageSchema("1xA5 on A4 in the middle", a4page, locations2);

        ArrayList<PageRectangle> locations = new ArrayList<PageRectangle>();
        locations.add(new PageRectangle(0, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        locations.add(new PageRectangle(a4page.getWidth() / 2, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        SinglePageSchema pageSchema2 = new SinglePageSchema("2xA5 on A4", a4page, locations);

        // ------------ Input Files --------------
        List<File> files = new LinkedList<File>();
        files.add(new File("src/test/resources/test-pdf-1.pdf"));
        files.add(new File("src/test/resources/test-pdf-2.pdf"));
        // files.add(new File("src/test/resources/test-pdf-img.pdf"));

        // Load source documents and retrieve document sizes
        int[] fileSizes = new int[files.size()];
        int i = 0;
        for (File file : files) {
            try {
                PDDocument pdd = PDDocument.loadNonSeq(file, null);
                fileSizes[i] = pdd.getNumberOfPages();
                System.out.println("File info: " + fileSizes[i] + "(pages)");
                pdd.close();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            i++;
        }
        // ---------- Create Selectors and lists ----------
        FilePageSelector firstPageSelector = new RangePageSelector(0, 0);
        FilePageSelector everyFourthFromSecondPageSelector = new CompoundFilePageSelector(new RangePageSelector(1), new LeapingPageSelector(4));

        // ---------- Create OneFileDocumentPatterns ----------
        OneFileDocumentSchemaPattern oneFileDocPat1 = new SimpleOneFileDocumentSchemaPattern(pageSchema1, firstPageSelector);
        OneFileDocumentSchemaPattern oneFileDocPat2 = new SimpleOneFileDocumentSchemaPattern(pageSchema2, everyFourthFromSecondPageSelector);
        List<OneFileDocumentSchemaPattern> oneFilePatterns = new LinkedList<OneFileDocumentSchemaPattern>();
        oneFilePatterns.add(oneFileDocPat1);
        oneFilePatterns.add(oneFileDocPat2);

        // ---------- Create Automatic Document Schema and use it ----------
        AutomaticDocumentSchemaPattern automaticDocPattern = new AutomaticDocumentSchemaPattern(oneFilePatterns);
        DocumentSchema docSchema = automaticDocPattern.produceDocumentSchema(fileSizes);
        CompoundSettingExample.compile(docSchema, files, "out-test/out-automatic-test.pdf");
    }
}
